package cn.itcast.bos.web.action;


import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.core.MediaType;

import org.apache.commons.io.FileUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.constant.Constants;
import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.domain.page.PageBean;
import cn.itcast.bos.domain.take_delivery.Order;
import cn.itcast.bos.domain.take_delivery.Promotion;
import cn.itcast.bos.web.action.common.BaseAction;
import cn.itcast.crm.domain.Customer;
import freemarker.template.Configuration;
import freemarker.template.Template;

@ParentPackage(value="json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class OrderAction extends BaseAction<Order>{

	private String sendAreaInfo;
	private String recAreaInfo;

	public void setSendAreaInfo(String sendAreaInfo) {
		this.sendAreaInfo = sendAreaInfo;
	}
	public void setRecAreaInfo(String recAreaInfo) {
		this.recAreaInfo = recAreaInfo;
	}

	/**
	 * 前台订单保存
	 * @return
	 * @throws Exception
	 */
	@Action(value = "order_add",results={@Result(name="success",type="redirect",location="index.html")})
	public String  add() {
		//System.out.println("你好啊");
		//手动封装Area
		Area sendArea = new Area();
		String[] sendAreaInfoArray = sendAreaInfo.split("/");
		sendArea.setProvince(sendAreaInfoArray[0]);
		sendArea.setCity(sendAreaInfoArray[1]);
		sendArea.setDistrict(sendAreaInfoArray[2]);
		Area recArea = new Area();
		String[] recAreaInfoArray = recAreaInfo.split("/");
		sendArea.setProvince(recAreaInfoArray[0]);
		sendArea.setCity(recAreaInfoArray[1]);
		sendArea.setDistrict(recAreaInfoArray[2]);
		model.setSendArea(sendArea);
		model.setRecArea(recArea);
		
		Customer customer=(Customer) ServletActionContext.getRequest().getSession().getAttribute("customer");
		model.setCustomer_id(customer.getId());
		//调用Webservice将数据传递给bos_management
//		WebClient
//		.create(Constants.BOS_MANAGEMENT_URL+
//				"/services/orderService/order")
//				.type(MediaType.APPLICATION_JSON)
//				.post(model);
		WebClient
		.create(Constants.BOS_MANAGEMENT_URL 
				+ "/services/orderService/order")
		.type(MediaType.APPLICATION_JSON).post(model);
		
		
		return SUCCESS;
		
	}
	
		
		
}









